<template>
  <div class="matain">
    <div class="list-title">
      <div class="left">
        <p>所有商品</p>
        <span>{{count}}</span>
      </div>
      <div class="right"></div>
    </div>
    <div class="order-box">
      <van-radio-group v-model="sort" direction="horizontal">
        <van-radio name="id">新品</van-radio>
        <van-radio name="goodsnum">销量</van-radio>
      </van-radio-group>
      <van-radio-group v-model="order" direction="horizontal">
        <van-radio name="asc">价格由低到高</van-radio>
        <van-radio name="desc">价格由高到低</van-radio>
      </van-radio-group>
    </div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="flag"
      >
        <ul class="ull">
          <!-- 上拉加载 -->

          <li v-for="(item,index) in list" :key="index" @click="gotoDedail(item.id)">
            <img :src="item.goodsimg" alt>
            <p class="nanxie">{{item.goodsname}}</p>
            <p class="price">￥{{item.goodsprice}}</p>
          </li>
        </ul>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import goodsinfoModel from "../model/goodsinfoModel";
export default {
  data() {
    return {
      list: [],
      count: null,
      sort: "id",
      order: "asc",
      page: 1,
      limit: 6,
      loading: false,
      flag: false,
      finished: false,
      refreshing: false
    };
  },
  watch: {
    sort() {
      this.finished = false;
      if (this.sort == "id") {
        this.list.sort((a, b) => a.id - b.id);
      } else {
        this.list.sort((a, b) => a.goodsnum - b.goodsnum);
      }
    },
    order() {
      this.finished = false;
      if (this.order == "asc") {
        this.list.sort((a, b) => a.goodsprice - b.goodsprice);
      } else {
        this.list.sort((a, b) => b.goodsprice - a.goodsprice);
      }
    }
  },

  methods: {
    gotoDedail(id) {
      this.finished = false;
      this.$router.push("/dedail/" + id);
    },

    
    async search() {
      this.finished = false;
      let { data: list2 } = await goodsinfoModel.query({
        _page: this.page,
        _limit: this.limit,
        _sort: this.sort,
        _order: this.order
      });
      if (this.page == 1) {
        this.list = list2; //赋值
      } else {
        this.list = [...this.list, ...list2];
      }
      let { data: list3 } = await goodsinfoModel.query({}); //为了防止this.count为null
      this.count = list3.length;
      this.loading = false;
      if (this.list.length >= this.count) {
        this.finished = true;
      }
    },
    async onRefresh() {
      this.refreshing = false;
      this.page = 1;

      this.search();
      //表示可以再次请求
    },
    onLoad() {
      console.log("加载数据");
      this.finished = false;
      this.page++;
      this.search();
    }
  },
  async created() {
    this.finished = false;
    let { data: list1 } = await goodsinfoModel.query({
      _page: this.page,
      _limit: this.limit,
      _sort: this.sort,
      _order: this.order
    });
    console.log(list1);
    console.log(5555);
    this.list = list1;
    let { data: list3 } = await goodsinfoModel.query({});
    this.count = list3.length;
    console.log(list3.length);
  }
};
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
.matain {
  margin-top: 54px;
}
.list-title {
  width: 100%;
  height: 40px;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.left p {
  display: inline-block;
  height: 40px;
  line-height: 40px;
  font-size: 16px;
  font-weight: 600;
}
.left span {
  color: #999;
  display: inline-block;
  font-size: 13px;
  margin-left: 3px;
  font-weight: normal;
}
.right {
  width: 50px;
  height: 32px;
  display: block;
  overflow: hidden;
  background: url(https://img.fishfay.com/theme/images/icons/filter-b.svg)
    no-repeat;
  background-size: auto 20px;
  background-position: center;
}
.ull {
  width: 100%;
  padding: 0 10px;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
}
.ull li {
  width: 50%;
  padding: 10px 5px;

  box-sizing: border-box;
}
.ull li .nanxie {
  width: 100%;
  height: 26px;
  text-align: center;
  line-height: 26px;
  font-size: 14px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ull li .price {
  color: #000;
  font-weight: 500;
  font-size: 15px;
  line-height: 26px;
  height: 26px;
}
/* .order-box {
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.order-box span {
  display: inline-block;
  font-size: 14px;
  color: #777;
} */
</style>
